import { get, post, put, del, dishService } from './request'
import type { ApiResponse } from './user'

// 菜品相关接口类型定义
export interface Dish {
  id: number
  name: string
  price: number
  image: string
  description: string
  categoryId: number
  isSpecial: number
  specialPrice: number
  isRecommended: number
  merchantId: number
  status: number
  createTime?: string
  updateTime?: string
}

export interface DishCategory {
  id: number
  name: string
  icon: string
  sort: number
  merchantId: number
  status: number
  createTime?: string
  updateTime?: string
}

export interface DishSpec {
  id: number
  name: string
  price: number
  dishId: number
  sort: number
  status: number
  createTime?: string
  updateTime?: string
}

export interface DishWithSpecs extends Dish {
  specs?: DishSpec[]
}

export interface CreateDishParams {
  name: string
  price: number
  image: string
  description: string
  categoryId: number
  isSpecial?: number
  specialPrice?: number
  isRecommended?: number
  merchantId: number
  status?: number
  specs?: {
    name: string
    price: number
    sort: number
  }[]
}

export interface UpdateDishParams {
  name?: string
  price?: number
  image?: string
  description?: string
  categoryId?: number
  isSpecial?: number
  specialPrice?: number
  isRecommended?: number
  status?: number
}

export interface CreateDishCategoryParams {
  name: string
  icon: string
  sort: number
  merchantId: number
  status?: number
}

export interface UpdateDishCategoryParams {
  name?: string
  icon?: string
  sort?: number
  status?: number
  merchantId?: number
}

export interface CreateDishSpecParams {
  name: string
  price: number
  dishId: number
  sort: number
  status?: number
}

export interface UpdateDishSpecParams {
  name?: string
  price?: number
  sort?: number
  status?: number
}

// 菜品相关API
// 创建菜品
export function createDish(data: CreateDishParams) {
  return post<ApiResponse<Dish>>(dishService, '/dishes', data)
}

// 获取商家菜品列表（认证接口）
export function getMerchantDishes() {
  return get<ApiResponse<Dish[]>>(dishService, '/dishes')
}

// 更新菜品
export function updateDish(id: number, data: UpdateDishParams) {
  return put<ApiResponse<Dish>>(dishService, `/dishes/${id}`, data)
}

// 获取菜品详情
export function getDishById(id: number) {
  return get<ApiResponse<Dish>>(dishService, `/dishes/${id}`)
}

// 删除菜品
export function deleteDish(id: number) {
  return del<ApiResponse<void>>(dishService, `/dishes/${id}`)
}

// 获取商家菜品列表（公开接口）
export function getDishByMerchantId(merchantId: number) {
  return get<ApiResponse<Dish[]>>(dishService, `/dishes/merchant/${merchantId}`)
}

// 根据分类获取菜品列表
export function getDishByCategoryId(categoryId: number) {
  return get<ApiResponse<Dish[]>>(dishService, `/dishes/category/${categoryId}`)
}

// 分页获取商家菜品列表
export function getDishListByMerchantId(merchantId: number, page: number = 1, size: number = 10) {
  return get<ApiResponse<{
    content: Dish[]
    totalElements: number
    totalPages: number
  }>>(dishService, '/dishes/list/merchant', { merchantId, page, size })
}

// 分页获取分类菜品列表
export function getDishListByCategoryId(categoryId: number, page: number = 1, size: number = 10) {
  return get<ApiResponse<{
    content: Dish[]
    totalElements: number
    totalPages: number
  }>>(dishService, '/dishes/list/category', { categoryId, page, size })
}

// 菜品名称搜索
export function searchDishByName(name: string, page: number = 1, size: number = 10) {
  return get<ApiResponse<{
    content: Dish[]
    totalElements: number
    totalPages: number
  }>>(dishService, '/dishes/search', { name, page, size })
}

// 商家菜品名称搜索
export function searchDishByMerchantIdAndName(merchantId: number, name: string, page: number = 1, size: number = 10) {
  return get<ApiResponse<{
    content: Dish[]
    totalElements: number
    totalPages: number
  }>>(dishService, '/dishes/search/merchant', { merchantId, name, page, size })
}

// 更新菜品状态
export function updateDishStatus(id: number, status: number) {
  return put<ApiResponse<Dish>>(dishService, `/dishes/${id}/status?status=${status}`)
}

// 更新菜品推荐状态
export function updateDishRecommendedStatus(id: number, isRecommended: number) {
  return put<ApiResponse<Dish>>(dishService, `/dishes/${id}/recommended?isRecommended=${isRecommended}`)
}

// 更新菜品特价状态
export function updateDishSpecialStatus(id: number, isSpecial: number, specialPrice: number) {
  return put<ApiResponse<Dish>>(dishService, `/dishes/${id}/special?isSpecial=${isSpecial}&specialPrice=${specialPrice}`)
}

// 获取商家推荐菜品
export function getRecommendedDishByMerchantId(merchantId: number) {
  return get<ApiResponse<Dish[]>>(dishService, `/dishes/recommended/${merchantId}`)
}

// 获取商家特价菜品
export function getSpecialDishByMerchantId(merchantId: number) {
  return get<ApiResponse<Dish[]>>(dishService, `/dishes/special/${merchantId}`)
}

// 菜品分类相关API
// 创建菜品分类
export function createDishCategory(data: CreateDishCategoryParams) {
  return post<ApiResponse<DishCategory>>(dishService, '/dish-categories', data)
}

// 获取商家菜品分类列表（认证接口）
export function getMerchantDishCategories() {
  return get<ApiResponse<DishCategory[]>>(dishService, '/dish-categories')
}

// 更新菜品分类
export function updateDishCategory(id: number, data: UpdateDishCategoryParams) {
  return put<ApiResponse<DishCategory>>(dishService, `/dish-categories/${id}`, data)
}

// 获取菜品分类详情
export function getDishCategoryById(id: number) {
  return get<ApiResponse<DishCategory>>(dishService, `/dish-categories/${id}`)
}

// 删除菜品分类
export function deleteDishCategory(id: number) {
  return del<ApiResponse<void>>(dishService, `/dish-categories/${id}`)
}

// 获取商家菜品分类列表（公开接口）
export function getDishCategoryByMerchantId(merchantId: number) {
  return get<ApiResponse<DishCategory[]>>(dishService, `/dish-categories/merchant/${merchantId}`)
}

// 分页获取商家菜品分类列表
export function getDishCategoryListByMerchantId(merchantId: number, page: number = 1, size: number = 10) {
  return get<ApiResponse<{
    content: DishCategory[]
    totalElements: number
    totalPages: number
  }>>(dishService, '/dish-categories/list', { merchantId, page, size })
}

// 更新菜品分类状态
export function updateDishCategoryStatus(id: number, status: number) {
  return put<ApiResponse<DishCategory>>(dishService, `/dish-categories/${id}/status?status=${status}`)
}

// 菜品规格相关API
// 创建菜品规格
export function createDishSpec(data: CreateDishSpecParams) {
  return post<ApiResponse<DishSpec>>(dishService, '/dish-specs', data)
}

// 更新菜品规格
export function updateDishSpec(id: number, data: UpdateDishSpecParams) {
  return put<ApiResponse<DishSpec>>(dishService, `/dish-specs/${id}`, data)
}

// 获取菜品规格详情
export function getDishSpecById(id: number) {
  return get<ApiResponse<DishSpec>>(dishService, `/dish-specs/${id}`)
}

// 删除菜品规格
export function deleteDishSpec(id: number) {
  return del<ApiResponse<void>>(dishService, `/dish-specs/${id}`)
}

// 获取菜品规格列表
export function getDishSpecByDishId(dishId: number) {
  return get<ApiResponse<DishSpec[]>>(dishService, `/dish-specs/dish/${dishId}`)
} 